$(function () {

    // 评论渲染
    function getComment(page, callData) {
        $.ajax({
            type: 'get',
            url: BigNew.comment_list,
            data: {
                page,
                perpage: 10
            },
            success(backData) {
                console.log(backData);
                if (backData.code == 200) {
                    let resHtml = template('commenTemp', backData.data);
                    // console.log(resHtml);
                    $('tbody').html(resHtml);
                    if (callData) {
                        callData(backData.data)
                    } else {
                        //评论分页
                        let $twbs = $('#page').twbsPagination({
                            totalPages: backData.data.totalPage,
                            visiblePages: 5,
                            first: '首页',
                            prev: '上一页',
                            next: '下一页',
                            last: '尾页',
                            onPageClick: function (event, page) {
                                getComment(page)
                            }

                        });
                    }
                };
            },
        });
    };
    // 评论渲染 调用
    getComment(1);

    // 评论审核函数
    function auditCom(ele, url) {
        let id = ele.parent().attr('data-id')
        $.ajax({
            type: 'post',
            url,
            data: {
                id
            },
            success(backData) {
                console.log(backData);
                if (backData.code == 200) {
                    // 获取当前页
                    let currentPage = $('#page').twbsPagination('getCurrentPage');
                    getComment(currentPage)
                };
            },
        });
    }
    // 评论审核通过
    $('tbody').on('click', '.btn-pass', function () {
        auditCom($(this), BigNew.comment_pass)
    });
    // 评论审核不通过
    $('tbody').on('click', '.btn-reject', function () {
        auditCom($(this), BigNew.comment_reject)
    });

    // 删除评论
    $('tbody').on('click', '.btn-delete', function () {
        // auditCom($(this), BigNew.comment_reject)
        let id = $(this).parent().attr('data-id')
        $.ajax({
            type: 'post',
            url: BigNew.comment_delete,
            data: {
                id
            },
            success(backData) {
                console.log(backData);
                if (backData.code == 200) {
                    //获取当前页
                    let currentPage = $('#page').twbsPagination('getCurrentPage')

                    getComment(currentPage, function (backData) {

                        let totalPage = backData.totalPage

                        if (totalPage < currentPage) {
                            currentPage = totalPage;
                        };
                        // 设置当前页
                        $('#page').twbsPagination('changeTotalPages', totalPage, currentPage)
                    });
                };
            },
        });
    });
















































});